<?php

namespace App\depository;

use App\Models\ManageForbiddenModel;

class ManageForbiddenDep
{
    public $model;

    /**
     * @var 已通过
     */
    CONST STATUS_ONE = "1";
    /**
     * @var 未审核
     */
    CONST STATUS_TWO = "2";
    /**
     * @var 免审核
     */
    CONST STATUS_THREE = "3";
    /**
     * @var 已拒绝
     */
    CONST STATUS_FOROUR = "4";
    /**
     * @var 已解封
     */
    CONST STATUS_FIVE = "5";
    /**
     * @var 已过期
     */
    CONST STATUS_SIX = "6";

    CONST STATUS = [
        self::STATUS_ONE => '已通过',
        self::STATUS_TWO => '未审核',
        self::STATUS_THREE => '免审核',
        self::STATUS_FOROUR => '已拒绝',
        self::STATUS_FIVE => '已解封',
        self::STATUS_SIX => '已过期'
    ];

    public function __construct(){
        $this->model = new ManageForbiddenModel();
    }

    public function first($id){
        $res = $this->model->where('id',$id)->first();
        return $res;
    }

    public function add($data){
        $res = $this->model->insert($data);
        return $res;
    }

    public function getByStatus($status){
        $res = $this->model->whereIn('status',$status)->get();
        return $res;
    }

    public function get_list_by_steamid($steamid){
        $res = $this->model->select(['steamid', 'days', 'reason', 'start_time', 'end_time','status'])->where('steamid', $steamid)->orderBy('id','desc')->get()->toArray();
        return $res;
    }

    public function get_list_by_status($gh_id,$fields = ['steamid'], $status = [self::STATUS_ONE, self::STATUS_THREE]){
        $res = $this->model->select($fields)->whereIn("status", $status)->where("gh_id", $gh_id)->orderBy('id','desc')->get()->toArray();
        return $res;
    }

    public function getBySteamIdStatus($steamid,$status){
        $res = $this->model->where('steamid', $steamid)->whereIn('status',$status)->orderBy('id','desc')->get();
        return $res;
    }

    public function list($param)
    {
        $res = $this->model
            ->where('gh_id', $param['gh_id'])
            ->when(!empty($param['id']), function ($query) use ($param) {
                 $query->where("id",$param['id']);
            })
            ->when(!empty($param['steamid']), function ($query) use ($param) {
                 $query->where("steamid", $param['steamid']);
            })
            ->when(!empty($param['status']), function ($query) use ($param) {
                 $query->where("status", $param['status']);
            })
            ->orderBy('id', 'desc')
            ->paginate($param['per_page'], ['*'], 'page', $param['current_page']);;
        return $res;
    }

    public function edit($param)
    {
        $this->model->where('id', $param['id'])->update([
            'reason' => $param['reason']
        ]);
    }

    public function update($id,$data){
        $res = $this->model->where('id', $id)->update($data);
        return $res;
    }

    public function del($param)
    {
        $this->model->where('id', $param['id'])->delete();
    }


}
